package com.gtt.lazy.controller;

import java.util.List;

import com.gtt.lazy.domain.Category;
import com.gtt.lazy.domain.CategoryToSofa;
import com.gtt.lazy.domain.Sofa;
import com.gtt.lazy.service.CategoryService;
import com.gtt.lazy.service.CategorySofaService;
import com.gtt.lazy.service.SofaService;

/**
 * 类别沙发关联管理Action
 * 
 * @author 高甜甜
 * @date 2009-7-9
 */
public class CatSofaAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private CategorySofaService service;
	private CategoryService categoryService;
	private SofaService sofaService;

	private List<Sofa> results;
	private List<Category> cats;
	private List<Sofa> sofas;

	private int cid;
	private int sid;
	private int[] sids;

	@SuppressWarnings("unchecked")
	public String list() {
		listCategories();
		if (cats.size() == 0)
			return SUCCESS;
		// 初始进入
		if (cid == 0 && !getSession().containsKey("ccid")) {
			cid = cats.get(0).getId();
		}
		// 翻页或添加关联后转入
		if (cid == 0 && getSession().containsKey("ccid")) {
			cid = (Integer) getSession().get("ccid");
		}
		// 确定所要显示的页数
		if (page == 0)
			page = 1;
		int start = (page - 1) * Constant.SIZE;
		results = service.listSofas(cid, start, Constant.SIZE);
		setPagination(service.countSofas(cid), Constant.SIZE, page);
		getSession().put("ccid", cid);
		return SUCCESS;
	}

	public String toAdd() {
		listCategories();
		sofas = sofaService.list();
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String add() {
		if (sids == null)
			return SUCCESS;
		for (int sid : sids) {
			boolean exist = service.checkExist(new CategoryToSofa(cid, sid));
			if (!exist) {
				service.add(new CategoryToSofa(cid, sid));
			}
		}
		getSession().put("ccid", cid);
		return SUCCESS;
	}

	public String del() {
		cid = (Integer) getSession().get("ccid");
		service.delete(new CategoryToSofa(cid, sid));
		return SUCCESS;
	}

	private void listCategories() {
		cats = categoryService.list();
	}

	public List<Category> getCats() {
		return cats;
	}

	public void setCats(List<Category> cats) {
		this.cats = cats;
	}

	public List<Sofa> getResults() {
		return results;
	}

	public void setResults(List<Sofa> results) {
		this.results = results;
	}

	public List<Sofa> getSofas() {
		return sofas;
	}

	public void setSofas(List<Sofa> sofas) {
		this.sofas = sofas;
	}

	public int[] getSids() {
		return sids;
	}

	public void setSids(int[] sids) {
		this.sids = sids;
	}

	public int getCid() {
		return cid;
	}

	public void setCid(int cid) {
		this.cid = cid;
	}

	public int getSid() {
		return sid;
	}

	public void setSid(int sid) {
		this.sid = sid;
	}

	public SofaService getSofaService() {
		return sofaService;
	}

	public void setSofaService(SofaService sofaService) {
		this.sofaService = sofaService;
	}

	public CategoryService getCategoryService() {
		return categoryService;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public CategorySofaService getService() {
		return service;
	}

	public void setService(CategorySofaService service) {
		this.service = service;
	}

}
